//
//  GHLoginViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMLoginViewController.h"
#import "UITextField+STInputLimit.h"
#import "TMForgotViewController.h"
#import "TMRegisterViewController.h"
#import "TMTabbarViewController.h"
#import "TMTabbarViewController.h"
#import "TMRegisterViewController.h"
#define textFiledBackgroundColor [UIColor colorWithRed:77/255.0 green:96/255.0 blue:129/255.0 alpha:1]
@interface TMLoginViewController ()<WXApiDelegate,TencentSessionDelegate>
@property(nonatomic, strong) UITextField                     *accountTextFiled;
@property(nonatomic, strong) UITextField                     *pwdTextFiled;
@property(nonatomic, strong) TencentOAuth                     *tencentOAuth ;
@end

@implementation TMLoginViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"登录";
    [self configSubView];
    [self addNotifacations];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldTextDidChangeNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)textFieldTextDidChangeNotification:(NSNotification*)notify{
    if (notify.object == self.pwdTextFiled) {
        [self.pwdTextFiled st_textInputLimitWithLength:16];
    }else if (notify.object == self.accountTextFiled){
        [self.accountTextFiled st_textInputLimitWithLength:11];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{

    
    UIView * accountView = [self configAccountTextFiled];
    accountView.st_top = 100;
    [self.view addSubview:accountView];
    
    UIView * pwdView = [self configpwdTextFiled];
    pwdView.st_top = accountView.st_bottom + 20;
    [self.view addSubview:pwdView];
    
    __weak typeof(self) weakSelf =  self;
    STButton * loginButton = [[STButton alloc] initWithFrame:CGRectMake(30, pwdView.st_bottom + 40, UIScreenWidth - 60, 50)
                                                       title:@"登录"
                                                  titleColor:[UIColor whiteColor]
                                                   titleFont:18
                                                cornerRadius:5
                                             backgroundColor:KL_BlueBackGroundColor
                                             backgroundImage:nil
                                                       image:nil];
    loginButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [loginButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLoginButton];
    }];
    [self.view addSubview:loginButton];
    
    STButton * forgotButton = [[STButton alloc] initWithFrame:CGRectMake(30 + 22, loginButton.st_bottom + 5, 100, 44)
                                                        title:@"忘记密码?"
                                                   titleColor:BL_BlueBackGroundColor
                                                    titleFont:13
                                                 cornerRadius:0
                                              backgroundColor:[UIColor clearColor]
                                              backgroundImage:nil
                                                        image:nil];
    forgotButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [forgotButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedforgotButton];
    }];
    forgotButton.right = loginButton.right;
     [self.view addSubview:forgotButton];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, loginButton.st_bottom + 80, 80, 14)
                                                     text:@"第三方登录"
                                                textColor:BL_firstTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.st_centerX = UIScreenWidth / 2;
    [self.view addSubview:titleLable];
    
    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(52, loginButton.st_bottom + 80, titleLable.st_left - 52, 1)];
    line1.st_centerY = titleLable.st_centerY;
    line1.backgroundColor = BL_lineColor;
    [self.view addSubview:line1];
    
    
    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(52, loginButton.st_bottom + 80, titleLable.st_left - 52, 1)];
    line2.st_centerY = titleLable.st_centerY;
    line2.st_right = loginButton.st_right;
    line2.backgroundColor = BL_lineColor;
    [self.view addSubview:line2];
    
    STButton * weChatrButton = [[STButton alloc] initWithFrame:CGRectMake(0, line2.st_bottom + 40, 50, 50)
                                                         title:nil
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:25
                                               backgroundColor:nil
                                               backgroundImage:[UIImage imageNamed:@"weixin"]
                                                         image:nil];
    weChatrButton.st_centerX = UIScreenWidth / 2;
    [weChatrButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedWeChatButton];
    }];
    [self.view addSubview:weChatrButton];
    
    STButton * qqButton = [[STButton alloc] initWithFrame:CGRectMake(0, line2.st_bottom + 40, 50, 50)
                                                    title:nil
                                               titleColor:nil
                                                titleFont:0
                                             cornerRadius:25
                                          backgroundColor:nil
                                          backgroundImage:[UIImage imageNamed:@"QQ"]
                                                    image:nil];
    qqButton.st_centerX = weChatrButton.st_left - 60;
    [qqButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedQQButton];
    }];
    [self.view addSubview:qqButton];
    
    STButton * sinaButton = [[STButton alloc] initWithFrame:CGRectMake(0, line2.st_bottom + 40, 50, 50)
                                                      title:nil
                                                 titleColor:nil
                                                  titleFont:0
                                               cornerRadius:25
                                            backgroundColor:nil
                                            backgroundImage:[UIImage imageNamed:@"sina"]
                                                      image:nil];
    sinaButton.st_centerX = weChatrButton.st_right + 60;
    [sinaButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedQQButton];
    }];
    [self.view addSubview:sinaButton];
    
    weChatrButton.center = sinaButton.center;
    sinaButton.hidden = YES;
}
- (UIView*)configAccountTextFiled{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 44)];
    view.backgroundColor = [UIColor clearColor];
    
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 17, 26)];
    leftImageView.image = [UIImage imageNamed:@"手机"];
    leftImageView.centerY = view.height/2;
    [view addSubview:leftImageView];
    
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, view.st_width - 50 - 18, 44)];
    textFiled.placeholder = @"请输入你的手机号";
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textFiled.placeholder attributes:@{NSForegroundColorAttributeName:BL_BlueBackGroundColor}];
    textFiled.keyboardType = UIKeyboardTypeNumberPad;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view addSubview:textFiled];
    self.accountTextFiled = textFiled;
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    textFiled.height = 43;
    UIView  * line = [[UIView alloc] initWithFrame:CGRectMake(textFiled.left, textFiled.bottom, textFiled.width, 0.5)];
    line.backgroundColor = BL_BlueBackGroundColor;
    [view addSubview:line];
    return view;
}
- (UIView*)configpwdTextFiled{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 44)];
    view.backgroundColor = [UIColor clearColor];
    
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 20, 26)];
    leftImageView.image = [UIImage imageNamed:@"密码(1)"];
    leftImageView.centerY = view.height/2;
    [view addSubview:leftImageView];
    
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, view.st_width - 50 - 18, 44)];
    textFiled.placeholder = @"请输入密码";
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textFiled.placeholder attributes:@{NSForegroundColorAttributeName:BL_BlueBackGroundColor}];
  //  textFiled.keyboardType = UIKeyboardTypeNumberPad;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view addSubview:textFiled];
    self.pwdTextFiled = textFiled;
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    textFiled.height = 43;
    UIView  * line = [[UIView alloc] initWithFrame:CGRectMake(textFiled.left, textFiled.bottom, textFiled.width, 0.5)];
    line.backgroundColor = BL_BlueBackGroundColor;
    [view addSubview:line];
    return view;
}
#pragma mark --Action Method
- (void)onSelectedforgotButton{
    [self.navigationController pushViewController:[TMForgotViewController new] animated:YES];
}
- (void)onSelectedLoginButton{

    if (self.accountTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if (self.accountTextFiled.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"手机号格式错误"];
        return;
    }
    if (self.pwdTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"登录中"];
    [self loginWithMobie:self.accountTextFiled.text pwssWord:self.pwdTextFiled.text];
}
- (void)onSelectedregisterButton{
    
    [self.navigationController pushViewController:[TMRegisterViewController new] animated:YES];
    //[self.navigationController.view st_showAnimationWithType:STAnimationTypekCATransitionFade];
}
- (void)onSelectedQQButton{
    //qq
    self.tencentOAuth =  [[TencentOAuth alloc] initWithAppId:TencentQQAppid andDelegate:self];
    NSArray * permissions = [NSArray arrayWithObjects:@"get_user_info", @"add_share", nil];
    [self.tencentOAuth authorize:permissions inSafari:NO];
    //[SVProgressHUD showInfoWithStatus:@"正在开发中，敬请期待"];
}
- (void)onSelectedWeChatButton{
    //微信登录
    //构造SendAuthReq结构体
    SendAuthReq* req = [[SendAuthReq alloc ] init];
    req.scope = @"snsapi_userinfo";
    req.state = wechatLoginIndefier ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}

- (void)onSelectedSinaButton{
    
}
#pragma mark --TencentSessionDelegate
- (void)tencentDidLogin{

    [self sendQQLoginRequestWithOpenid:self.tencentOAuth.openId];
    
}
- (void)tencentDidNotLogin:(BOOL)cancelled{
    if (cancelled) {
        [SVProgressHUD showSuccessWithStatus:@"用户点击取消按键,主动退出登录"];
    }else{
         [SVProgressHUD showSuccessWithStatus:@"其他原因， 导致登录失败"];
    }
}
- (void)tencentDidUpdate:(TencentOAuth *)tencentOAuth{
    
    
}
- (void)tencentFailedUpdate:(UpdateFailType)reason{
    
    
}
- (void)getUserInfoResponse:(APIResponse*) response{
    
}
#pragma mark --NetWork Method
- (void)loginWithMobie:(NSString *)mobile pwssWord:(NSString *)passWord{
    [SVProgressHUD showWithStatus:@"登录中"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (mobile.length) {
        [dic setObject:mobile forKey:@"mobile"];
    }else{
        NSLog(@"登录手机号为空");
        return;
    }
    if (passWord.length) {
        [dic setObject:passWord forKey:@"password"];
    }else{
        NSLog(@"登录密码为空");
        return;
    }
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/loginAction"];
    
    if (self.openid.length && self.type.length) {
        [dic setObject:self.openid forKey:@"openid"];
        [dic setObject:self.type forKey:@"type"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                                 NSDictionary * dic = [responseObject valueForKey:@"data"];
                                                 BLUserModel * model =  [BLUserModel mj_objectWithKeyValues:dic];
                                                 [[STUserManger defult] updateUserModel:model];
                                                 [self autoRegisterEmclintWithPhone:model.mobile];
                                                 if ([dic.allKeys containsObject:@"uid"]) {
                                                     NSString * userId = dic[@"uid"];
                                                     [[STUserManger defult] updateUserID:userId];
                                                 }
//                                                 if ([dic.allKeys containsObject:@"token"]) {
//                                                     NSString * token = dic[@"token"];
//                                                     [[STUserManger defult] updateToken:token];
//                                                 }
                                                 
                                                 [self.view endEditing:YES];
                                                 //更换rootViewControol
                                                 [UIApplication sharedApplication].keyWindow.rootViewController = [TMTabbarViewController new];
                                                 [[UIApplication sharedApplication].keyWindow st_showAnimationWithType:STAnimationTyperippleEffect duration:2];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)autoRegisterEmclintWithPhone:(NSString*)phone{
    [[EMClient sharedClient] registerWithUsername:phone password:EMClientPassword completion:^(NSString *aUsername, EMError *aError) {
        //不管 是否注册，直接异步登录
        [[EMClient sharedClient] loginWithUsername:phone password:EMClientPassword completion:^(NSString *aUsername, EMError *aError) {
            [[EMClient sharedClient].options setIsAutoLogin:YES ];
        }];
        //加好友
        EMError *error = [[EMClient sharedClient].contactManager addContact:IM_userAccount message:@"我想加您为好友"];
        if (!error) {
            NSLog(@"添加成功");
        }
    }];
}
- (void)sendQQLoginRequestWithOpenid:(NSString*)openid{
    
    //没手机，去绑定手机
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    [dic setObject:openid forKey:@"openid"];
    [dic setObject:@"1" forKey:@"type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/loginAction"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 
                                                 NSDictionary * dic = [responseObject valueForKey:@"data"];
                                                 if ([dic.allKeys containsObject:@"mobile"]) {
                                                     NSString * mobie = dic[@"mobile"];
                                                     if (mobie.length < 2) {
                                                         //没手机，去绑定手机
                                                         [SVProgressHUD showSuccessWithStatus:@"已绑定qq，请绑定手机号"];
                                                         TMRegisterViewController * vc = [TMRegisterViewController new];
                                                         vc.openid = self.tencentOAuth.openId;
                                                         vc.type = @"1";
                                                         NSString * userId = dic[@"uid"];
                                                         vc.userID =  userId;
                                                         UINavigationController * homeVc = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                         [homeVc pushViewController:vc animated:YES];
                                                         
                                                         return ;
                                                     }else{
                                                         if ([dic.allKeys containsObject:@"uid"]) {
                                                             NSString * userId = dic[@"uid"];
                                                             [[STUserManger defult] updateUserID:userId];
                                                         }
                                                         [SVProgressHUD showSuccessWithStatus:@"qq登录成功"];
                                                         [UIApplication sharedApplication].keyWindow.rootViewController = [TMTabbarViewController new];
                                                         [[UIApplication sharedApplication].keyWindow st_showAnimationWithType:STAnimationTyperippleEffect duration:2];
                                                     }
                                                 }
                                                 //更换rootViewControol


                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

